<?php

namespace MyBlog\Http\Controllers\backend;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use MyBlog\Models\Category;

class CategoryController extends BaseController
{
    public function index() {
        $categories = Category::all();
        return View::make('backend.categories', compact('categories'));
    }

    public function create() {
        $category = new Category();
        return View::make('backend.category_form', compact('category'));
    }

    public function store() {
        $input = Input::all();
        $validation = Validator::make($input, Category::$rules, Category::$msgs);
        if($validation->fails()) {
            return Redirect::back()
                ->withInput()
                ->withErrors($validation);
        }
        Category::create($input);
        return Redirect::to('backend/categories');
    }
    public function edit(Category $category) {
        return View::make('backend.category_form', compact('category'));
    }

    public function update(Category $category) {
        $input = Input::all();
        $validation = Validator::make($input, Category::$rules, Category::$msgs);
        if($validation->fails()) {
            return Redirect::back()
                ->withInput()
                ->withErrors($validation);
        }
        $category->update($input);

        return Redirect::to('backend/categories');

    }

    public function destroy(Category $category) {
    	$category->delete();
    	return redirect('backend/categories');
    }

    public function changeState(Category $category) {
		$disabled = Input::get('disabled');
		$category->disabled = $disabled;
		$category->save();
		
		return redirect('backend/categories');
    }
}
